package com.example.administrator.asdasd;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;


/**
 * Created by Administrator on 2016/3/29.
 */
public class MyAdapter extends BaseAdapter {
    private List<ItemBean> mList;
    private LayoutInflater mInflater;
    public Context mcontext;

    public MyAdapter(Context context, List<ItemBean> list) {
        mList = list;
        mcontext = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item, null);
            viewHolder.textView1 = (TextView) convertView.findViewById(R.id.it_et1);
            viewHolder.textView2 = (TextView) convertView.findViewById(R.id.it_et2);
            viewHolder.textView3 = (TextView) convertView.findViewById(R.id.it_et3);
            viewHolder.textView4 = (TextView) convertView.findViewById(R.id.it_et4);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ItemBean bean = mList.get(position);
        viewHolder.textView1.setText(bean.Itname);
        viewHolder.textView2.setText(bean.Itwigh);
        viewHolder.textView3.setText(bean.Itdizhi);
        viewHolder.textView4.setText(bean.Itnum);
        viewHolder.ba_btn2 = (Button) convertView.findViewById(R.id.it_btn2);
        viewHolder.ba_btn1 = (Button) convertView.findViewById(R.id.it_btn1);
        viewHolder.ba_btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLdm s = new SQLdm();
                SQLiteDatabase db = s.openDatabase(mcontext.getApplicationContext());
                String str1 = viewHolder.textView1.getText().toString();
                String str2 = viewHolder.textView4.getText().toString();
                String str3 = str1.substring(4);
                String str4 = str2.substring(5);

                Cursor c = db.rawQuery("select * from hztb where name=? and num=?", new String[]{str3, str4});
                ContentValues val = new ContentValues();
                val.put("hzs", 1);
                if (c != null) {
                    db.update("hztb", val, "name=? and num=?", new String[]{str3, str4});
                }
                val.clear();
                c.close();
                Log.e("asd", str3);
                Log.e("asd", str4);
                c = db.rawQuery("select * from cztb where name=? and num=?", new String[]{str3, str4});
                val.put("czs", 1);
                if (c != null) {
                    db.update("cztb", val, "name=? and num=?", new String[]{str3, str4});
                }
                val.clear();
                db.close();
            }
        });


        viewHolder.ba_btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = viewHolder.textView4.getText().toString();
                Intent intent = new Intent();
                intent.setAction("android.intent.action.CALL");
                intent.setData(Uri.parse(str));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mcontext.startActivity(intent);

            }
        });

        return convertView;
    }

    public class ViewHolder {
        public TextView textView1;
        public TextView textView2;
        public TextView textView3;
        public TextView textView4;
        public Button ba_btn2;
        public Button ba_btn1;
    }
}
